﻿//菜单类型: StaticForm-静态页面 MarkForm-页签 CategoryForm-分类 
var maxLayer=4;
var firstColor="red";
var colorAry=new Array();
var winReport;
colorAry["ViewForm"]="#008080";
colorAry["MarkForm"]="#800080";
var titleAry=new Array();
titleAry["ViewForm"]="视图";
titleAry["MarkForm"]="页签";
var preFirstOption=null;    

//查询servlet获得菜单列表
function fn_getMenuList(){
     var xmlSource = new ActiveXObject("Msxml2.DOMDocument");
     xmlSource.async = false; 	
     xmlSource.resolveExternals = false;  
     var vUrl=appPath+"/tree.crm?mode=0";
     xmlSource.load(vUrl);     
     var ViewList=document.all.ViewList;    
     ViewList.length=0;
     var firstIndex=-1;
     var nodes=xmlSource.getElementsByTagName("MenuNode");
     if(nodes.length>0){
		 for(var i=0;i<nodes.length;i++){
		    var ele=document.createElement("OPTION");
		    var vText = getLayerText(nodes[i])+nodes[i].getAttribute("name")+getActiveText(nodes[i]);
		    var vId=nodes[i].getAttribute("id");
		    var vValue = nodes[i].getAttribute("form")+"#"+vId;	     	    
		    ele.text=vText; 
		    ele.value=vValue;
		    ViewList.add(ele);
		    if(nodes[i].getAttribute("isfirstitem")=="1"){	  	     	
		     	firstIndex=i;	     	
		     	document.getElementById("firstMenuItem").value=vId;
		    }
		 }	   
    }    
    changeItemColor();
    if(firstIndex!=-1){
      ViewList.options[firstIndex].style.color=firstColor;	     
      preFirstOption = ViewList.options[firstIndex];
    }
    fn_addViewTurn();
  }
  
 // 获得select option的带层次text属性
 function getLayerText(node){
    var parent = node.parentNode;
    var preText="";
    while(parent.tagName=="MenuNode"){
      preText+="-> ";
      parent=parent.parentNode; 
    }   
    return preText; 
  }  
  
  // 获得select option的活动text属性
  function getActiveText(node){
     var IsActive = node.getAttribute("isActive");
     return IsActive=="1"?"":" [非活动]";  
  }

//新增
function fn_newProcess(){
	var vIndex = document.all.ViewList.selectedIndex;
	var vUrl = appPath+"/menu.crm?method=init&formType=MarkForm&layer="+fn_getItemLayer(vIndex)+"&sortNumber="+vIndex;  
	window.open(vUrl,"",fnGetWinStatus(600,520));
}

//修改
function fn_editProcess(){
  var vTag=document.all.ViewList.options[document.all.ViewList.selectedIndex];
  var vIndex=document.all.ViewList.selectedIndex;
  var vValue=vTag.value.split("#");  
  var vUrl = appPath+"/menu.crm?method=initModify&formType="+vValue[0]+"&keyName="+vValue[1]+"&layer="+fn_getItemLayer(vIndex)+"&sortNumber="+vIndex;  
  window.open(vUrl,"",fnGetWinStatus(600,520));
}

//删除
function fn_deleteProcess(){
  if(document.all.ViewList.selectedIndex!=null)  
    if(confirm("请确认删除当前菜单项!")){      	
      var tag = document.all.ViewList.options[document.all.ViewList.selectedIndex];
      var options=document.all.ViewList.options;      
	  var vValue="";
	  for(var i=0;i<options.length;i++){	   	 
	   if(options[i].selected){
	    if(i==0 && options.length>1 && fn_getItemLayer(1)>0){
	    	alert("有子节点的首节点不能删除，请调整节点层次之后再删除!");
	    	return false;
	    }		
	    var tmp = options[i].value.split("#");
	    vValue+=vValue==""?tmp[1]:(","+tmp[1]);
	   } 
	  }
	  var vUrl=appPath+"/menu.crm?method=delete&id="+vValue;  	 
  	  var res=ajax_getAgentReturnHTML(vUrl);  	
      if(res==1){fn_getMenuList();}
   }
}  

//复制
function fn_copyProcess(){
  var tag = document.all.ViewList.options[document.all.ViewList.selectedIndex];
  var vIndex=document.all.ViewList.selectedIndex;
  var vValue=tag.value.split("#");
  var vUrl=appPath+"/menu.crm?method=copy&keyName="+vValue[1]+"&layer="+fn_getItemLayer(vIndex)+"&index="+vIndex;
  var res =ajax_getAgentReturnHTML(vUrl);
  if(res==1){fn_getMenuList();}
}

//设置首选项    
function fn_setFirstItem(){
 var ViewList=document.getElementById("ViewList");
 var firstMenuItem=document.getElementById("firstMenuItem");
 var tagOption=ViewList.options[ViewList.selectedIndex]; 
 var vId=tagOption.value.substring(tagOption.value.indexOf("#")+1); 
 if(firstMenuItem.value!=vId){
   firstMenuItem.value=vId;
   tagOption.style.color=firstColor;   
   if(preFirstOption!=null)
      fn_setColor(preFirstOption); 
      preFirstOption=tagOption;
 }
 else{
   firstMenuItem.value="";
   fn_setColor(tagOption);
   preFirstOption=null;
 }
}

//激活/失活
function fn_activeItem(){
  var ViewList=document.all.ViewList;
  var tagOption=ViewList.options[ViewList.selectedIndex];
  var vKey=tagOption.text.indexOf("[非活动]")!=-1?false:true;
  fn_activeOption(tagOption,vKey);
  //所有子项
  var vIndex=ViewList.selectedIndex;
  var curLayer=fn_getItemLayer(vIndex); 
  while(ViewList.options[vIndex+1] && fn_getItemLayer(vIndex+1)>curLayer){
     fn_activeOption(ViewList.options[vIndex+1],vKey);
     vIndex++;
  }
  tagOption.selected=true;
  fn_activeTitleChange();
  fn_addViewTurn();
}

function fn_activeOption(tagOption,vKey){
  if(vKey){
     if(tagOption.text.indexOf("[非活动]")==-1)    
      tagOption.text+=" [非活动]";
  }
  else
    tagOption.text=tagOption.text.replace(" [非活动]","");
}

function OptionMove(step){
   var vIndex=document.all.ViewList.selectedIndex;
   var Options=document.all.ViewList.options;
   var vNext=vIndex+step;  
   if((vIndex+step)>-1 && document.all.ViewList.options[vIndex+step]){
    var pre_Option=Options[vIndex+step]; 
    var cur_Option=Options[vIndex];
    pre_Option.swapNode(cur_Option);
    if(!fn_checkItemValid()){
       alert("非法移动!");     
       document.all.ViewList.selectedIndex=vNext;
       pre_Option.swapNode(cur_Option);
     }
   }
   fn_addViewTurn();
}

function OptionMoveLayer(step){
   var ViewList=document.all.ViewList;
   var vIndex=ViewList.selectedIndex;

   //初始根节点不做移动
   if(vIndex==0 && fn_getItemLayer(0)==0) return false;
   //最大层数限制
   if(step>0 && fn_getItemLayer(vIndex)>=maxLayer){alert("超过最大层数限制!!");return false}  

   if(step>0)
      ViewList.options[vIndex].text="-> "+ViewList.options[vIndex].text;     
   else
      ViewList.options[vIndex].text=ViewList.options[vIndex].text.replace("-> ","");
   
   if(!fn_checkItemValid()){
     alert("非法移动!");   
     if(step<0)
       ViewList.options[vIndex].text="-> "+ViewList.options[vIndex].text;     
     else
       ViewList.options[vIndex].text=ViewList.options[vIndex].text.replace("-> ","");   
   }

  ViewList.selectedIndex=vIndex;
  fn_addViewTurn();
}

function fn_addViewTurn(){
  var vOptions=document.all.ViewList.options;
  var vList="";  
  for(i=0;i<vOptions.length;i++)
  {   
    var vText = vOptions[i].text.replace(/-> /g,"");
    if(vText.indexOf("[")!=-1 && vText.indexOf("]")!=-1){      
      vText = vText.substring(0,vText.indexOf("[")); 
      var IsActive="0";
    }
    else{var IsActive="1";}      
    var item=i.toString()+"#"+fn_getItemLayer(i)+"#"+vText+"#"+vOptions[i].value.replace("/","#")+"#"+IsActive;    
    vList+=i==0?item:(","+item);}
    document.all.ViewTurn.value=vList;
}

//所有定义项目校验
function fn_checkItemValid(){
  var Options=document.all.ViewList.options;   
  if(fn_getItemLayer(0)>0){Options[0].selected=true;return false;}
  for(i=0;i<Options.length-1;i++){
    if(fn_getItemLayer(i+1)-fn_getItemLayer(i)>1){
      document.all.ViewList.selectedIndex=i;
      return false;
    }
  }
  return true;
}

//获得菜单层
function fn_getItemLayer(vIndex){  	
  if(vIndex==-1) return 0;	
  var vKey=document.all.ViewList.options[vIndex].text;
  if(vKey.indexOf("->")==-1) return 0;
  return vKey.split("->").length-1;
}

    function fn_showMenuTitle(){
     if(document.all.ViewList.selectedIndex==-1)return false;
	  var vOption=document.all.ViewList.options[document.all.ViewList.selectedIndex];
     var vKey=vOption.value.substring(0,vOption.value.indexOf("#"));
     document.all.menuTitle.innerText=titleAry[vKey];
     fn_activeTitleChange();
	}
    
 //切换按钮显示名称
function fn_activeTitleChange(){
  var ViewList=document.all.ViewList;
  var tagOption=ViewList.options[ViewList.selectedIndex];
  var vKey=tagOption.text.indexOf("[非活动]")!=-1?false:true;
  document.getElementById("btn_active").value=!vKey?"显示菜单":"隐藏菜单";
}

function changeItemColor(){
  var Options=document.all.ViewList.options;
  var color="#000000";
  for (i=0;i<Options.length;i++){
     fn_setColor(Options[i]);        
  }
}

function fn_setColor(vOption){
  if(!vOption || vOption.value.indexOf("#")==-1) return false; 
  vOption.style.color=colorAry[vOption.value.substring(0,vOption.value.indexOf("#"))];
}